package com.ninthgroup.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninthgroup.entity.Role;
import com.ninthgroup.mapper.RoleMapper;
import com.ninthgroup.service.RoleService;
import com.ninthgroup.vo.Result;
import com.ninthgroup.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：1452863884@qq.com rcg
 * @date ：Created in 2022/10/15 13:32
 * @description：角色的接口实现类
 * @modified By：
 * @version:1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Override
    public Result findByCondition(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Role> roles=roleMapper.selectByCondition(roleVo.getRoleName(),roleVo.getRoleDesc(),roleVo.getRolePosition());
        PageInfo<Role> pageInfo=new PageInfo<>(roles);
        return new Result(2000,"查询成功",pageInfo);
    }



    @Override
    public Result findByDoctorId(Integer dId) {
        //1.查询所有的角色
        List<Role> list= roleMapper.selectAll();
        //2.查询用户具有的角色id
        List<String> roleIds= roleMapper.findRoleIdByDoctorId(dId);

        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("roleIds",roleIds);
        return new Result(2000,"查询成功",map);
    }

    @Override
    public Result qr(Integer dId, Integer[] roleIds) {
        //1.根据用户删除该用户具有的角色id
        roleMapper.deleteByDoctorId(dId);
        //2.添加新的角色id
        roleMapper.insertRoleId(dId,roleIds);
        return new Result(2000,"添加成功");
    }

    @Override
    public Result deleteRole(Integer roleId) {
        int i = roleMapper.deleteByPrimaryKey(roleId);
        return new Result(200,"删除角色成功");
    }

    @Override
    public Result updateRole(Role role) {
        int i = roleMapper.updateByPrimaryKeySelective(role);

        return new Result(200,"修改角色成功");
    }


}